package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @Author Jqcc
 * @Version 1.0
 * @Data 2023/8/12
 */
@Mapper
public interface AddressBookMapper {

    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return
     */
    @Select("select * from address_book where user_id = #{userId}")
    List<AddressBook> getAddressBookByUserId(Long userId);

    /**
     * 新增地址
     *
     * @param addressBook
     */
    void insert(AddressBook addressBook);

    /**
     * 查询默认地址
     *
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId} and is_default = 1")
    AddressBook getDefaultAddressByUserId(Long userId);


    /**
     * 更改原有的默认地址
     *
     * @param userId
     */
    @Update("update address_book set is_default = 0 where user_id = #{id}")
    void updateDefaultAddressByUserId(Long userId);


    /**
     * 设置默认地址
     *
     * @param userId
     * @param id
     */
    @Update("update address_book set is_default = 1 where user_id = #{userId} and id = #{id}")
    void setDefaultAddressByUserId(Long userId, Long id);


    /**
     * 根据id修改地址
     *
     * @param addressBook
     */
    void updateById(AddressBook addressBook);


    /**
     * 根据id查询地址
     *
     * @param id     地址id
     * @param userId 用户id
     * @return
     */
    @Select("select * from address_book where id = #{id}")
    AddressBook getAddressById(Long id);


    /**
     * 根据id删除地址
     *
     * @param id     地址id
     * @param userId 用户id
     */
    @Delete("delete from address_book where id = #{id} and user_id = #{userId}")
    void deleteAddressById(Long id, Long userId);
}
